##############################################################################
#                                                                            #
# Copyright (c) 2008 Brenno de Winter (brenno@dewinter.com)                  #
# Copyright (c) 2007-2008 Bernd Kreuss <prof7bit@gmail.com>                  #
# Small Sister Project: http://www.smallsister.org/                          #
#                                                                            #
# This program is licensed under the GNU General Public License V3,          #
# the full source code is included in the binary distribution.               #
#                                                                            #
# Included in the distribution are files from other open source projects:    #
# - TOR Onion Router (c) The Tor Project, 3-clause-BSD                       #
# - SocksiPy (c) Dan Haim, BSD Style License                                 #
# - Gajim buddy status icons (c) The Gajim Team, GNU GPL                     #
#                                                                            #
##############################################################################

#Import system modules
import os
import sys

def getScriptDir():
    #must be called at least once before working dir is changed
    #because after that abspath won't work correctly anymore.
    #this is the reason why this function uses a cache.
    global _script_dir
    try:
        return _script_dir
    except:
        #first call, _script_dir not yet defined
        _script_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
        return _script_dir

class LogWriter:
    def __init__(self):
        self.stdout = sys.stdout
        sys.stdout = self
        sys.stderr = self
        self.level = config.getint("logging", "log_level")
        self.file_name = config.get("logging", "log_file")
        if  self.level and self.file_name:
            try:
                self.logfile = open(self.file_name, 'w')
                print "(1) started logging to file '%s'" % os.path.abspath(self.file_name)
            except:
                tb(0)
                print "(0) could not open logfile '%s'" % os.path.abspath(self.file_name)
                print "(0) logging only to stdout"
        print "(1) current log level is '%i'" % self.level
